<?php
/**
 * User: Feng
 * Date: 2024/08/18
 */

namespace app\common\model;

class Company extends Common
{
	public function tableData($post)
	{
		if (isset($post['limit'])) {
			$limit = $post['limit'];
		} else {
			$limit = config('paginate.list_rows');
		}
		$tableWhere = $this->tableWhere($post);
		$list = $this->field('*')->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
		foreach ($list as $key => $val) {
			$list[$key]['pic'] = _sImage($val['pic']);
			$list[$key]['code_pic'] = _sImage($val['code_pic']);
			$list[$key]['position'] = config('params.company')['position'][$val['position']];
		}
		$data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
		$re['code'] = 0;
		$re['msg'] = '';
		$re['count'] = $list->total();
		$re['data'] = $data;
		return $re;
	}

	public function addData($data)
	{
		$result = [
			'status' => true,
			'msg'    => '保存成功',
			'data'   => ''
		];
		if (! $this->allowField(true)->save($data)) {
			$result['status'] = false;
			$result['msg'] = error_code(10004, true);
		}
		return $result;
	}

	public function saveData($data)
	{
		$result = [
			'status' => true,
			'msg'    => '保存成功',
			'data'   => ''
		];
		if (! $this->allowField(true)->save($data, ['id' => $data['id']])) {
			$result['status'] = false;
			$result['msg'] = error_code(10004, true);
		}
		return $result;
	}

	protected function tableWhere($post)
	{
		$where = [];
		if (isset($post['title']) && $post['title'] != "") {
			$where[] = [
				'title',
				'like',
				'%' . $post['title'] . '%'
			];
		}
		if (isset($post['type']) && $post['type'] != "") {
			$where[] = [
				'type',
				'eq',
				$post['type']
			];
		}
		$result['where'] = $where;
		$result['field'] = "*";
		$result['order'] = ['id' => 'ASC'];
		return $result;
	}
}